<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="Icons" name="1">
        <el-input
          v-model="svgName"
          placeholder="请输入图标名称"
          prefix-icon="el-icon-search"
          clearable
          style="margin-bottom: 5px"
          @clear="filterSvgIcons"
          @input.native="filterSvgIcons"
        />
        <div class="icon-container">
          <div v-for="item in svgIcons" :key="item" @click="selectedSvgIcon(item)">
            <svg-icon :icon-class="item" style="height: 30px; width: 16px" />
            <span class="svg-span">{{ item }}</span>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="Element-UI Icons" name="2">
        <el-input
          v-model="name"
          placeholder="请输入图标名称"
          prefix-icon="el-icon-search"
          clearable
          style="margin-bottom: 5px"
          @clear="filterIcons"
          @input.native="filterIcons"
        />
        <div class="icon-container">
          <div v-for="item in elementIcons" :key="item" @click="selectedIcon(item)">
            <i :class="'el-icon-' + item" style="height: 30px; width: 16px" />
            <span>{{ item }}</span>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import svgIcons from './svg-icons'
import elementIcons from './element-icons'
export default {
  name: 'IconSelect',
  data() {
    return {
      svgIcons,
      elementIcons,
      activeName: '2',
      svgName: undefined,
      name: undefined,
    }
  },
  methods: {
    selectedSvgIcon(name) {
      this.$emit("selected", name);
      document.body.click();
    },
    selectedIcon(name) {
      this.$emit("selected", "el-icon-" + name);
      document.body.click();
    },
    filterSvgIcons() {
      this.svgIcons = svgIcons;
      if (this.svgName) {
        this.svgIcons = this.svgIcons.filter(item => item.includes(this.svgName))
      }
    },
    filterIcons() {
      this.elementIcons = elementIcons;
      if (this.name) {
        this.elementIcons = this.elementIcons.filter(item => item.includes(this.name))
      }
    },
    reset() {
      this.name = '';
      this.svgName = '';
      this.svgIcons = svgIcons;
      this.elementIcons = elementIcons;
    }
  },
}
</script>
<style lang='scss' scoped>
.app-container {
  padding: 0;
  .icon-container {
    height: 200px;
    overflow-y: auto;
    div {
      height: 30px;
      line-height: 30px;
      margin-bottom: -5px;
      cursor: pointer;
      width: 33%;
      float: left;
      &:hover {
        background-color: rgba($color: #999, $alpha: 0.1);
      }
    }
    .svg-span {
      display: inline-block;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }
    span {
      margin-left: 5px;
    }
  }
}
</style>